package com.brsya.movie.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.brsya.base.base.BaseLayoutFragment;
import com.brsya.base.bean.PlayListBean;
import com.brsya.base.recycler.BaseAdapter;
import com.brsya.base.recycler.BaseViewHolder;
import com.brsya.movie.R;

import java.util.List;

/**
 * Created by Brsya
 * CreateDate: 2022/5/28  15:13
 * Description:
 * 选集fragment
 */
public class PlayTypeFragment extends BaseLayoutFragment {
    private LinearLayout llInfoTitle;
    private TextView tvNowType;

    private OnSelectTypeListener onSelectTypeListener;

    private List<PlayListBean.PlayerListDTO> infoList;

    private BaseAdapter<PlayListBean.PlayerListDTO> typeAdapter;

    private String selectType;
    private RecyclerView rlMovieType;

    public static PlayTypeFragment newInstance(List<PlayListBean.PlayerListDTO> infoList, OnSelectTypeListener onSelectTypeListener) {

        Bundle args = new Bundle();

        PlayTypeFragment fragment = new PlayTypeFragment();
        fragment.setArguments(args);
        fragment.infoList = infoList;
        fragment.onSelectTypeListener = onSelectTypeListener;
        return fragment;
    }

    @Override
    protected void initView() {
        setContentViewId(R.layout.fragment_play_type);
        llInfoTitle = findViewById(R.id.ll_info_title);
        tvNowType = findViewById(R.id.tv_now_type);
        rlMovieType = findViewById(R.id.rl_movie_type);
    }

    @Override
    protected void initData() {
        llInfoTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSelectTypeListener != null) {
                    onSelectTypeListener.onClose();
                }
            }
        });

        typeAdapter = new BaseAdapter<PlayListBean.PlayerListDTO>() {
            @Override
            public BaseViewHolder onCreate(ViewGroup parent, int viewType) {
                View itemView = LayoutInflater.from(getContext()).inflate(R.layout.item_play_type, parent, false);
                return new BaseViewHolder(itemView);
            }

            @Override
            public void onBind(BaseViewHolder viewHolder, int RealPosition, PlayListBean.PlayerListDTO data) {
                TextView tv = viewHolder.findViewById(R.id.tv_movie_type_name);
                tv.setText(data.getPlayerName());
            }
        };

        typeAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener<PlayListBean.PlayerListDTO>() {
            @Override
            public void onItemClick(int position, PlayListBean.PlayerListDTO data) {
                if (onSelectTypeListener != null) {
                    onSelectTypeListener.onSelectType(position, data);
                }
            }
        });

        rlMovieType.setLayoutManager(new LinearLayoutManager(getContext()));
        rlMovieType.setAdapter(typeAdapter);

        initList();
    }

    private void initList() {

        tvNowType.setText(selectType);

        typeAdapter.setDataList(infoList);
        //        if (infoList != null) {
        //            for (int i = 0; i < infoList.size(); i++) {
        //
        //                PlayListBean.PlayerListDTO.InfoListDTO infoListDTO = infoList.get(i);
        //
        //                ItemPartView itemPartView ;
        //
        //                if (itemPartViews.size() > i) {
        //                    itemPartView = itemPartViews.get(i);
        //                }else{
        //                    itemPartView = new ItemPartView(getContext());
        //                    itemPartView.setOnClickListener(new View.OnClickListener() {
        //                        @Override
        //                        public void onClick(View v) {
        //                            if (v instanceof ItemPartView) {
        //                                int index = ((ItemPartView) v).getIndex();
        //                                for (int i1 = 0; i1 < infoList.size(); i1++) {
        //                                    PlayListBean.PlayerListDTO.InfoListDTO infoListDTO1 = infoList.get(i1);
        //                                    if (i1 == index) {
        //                                        if (onSelectPartListener != null) {
        //                                            onSelectPartListener.onSelectPart(index, infoListDTO1);
        //                                        }
        //                                        ((ItemPartView) v).setPlayStatus(true);
        //                                    }else{
        //                                        ((ItemPartView) v).setPlayStatus(false);
        //                                    }
        //                                }
        //                                initPartList();
        //                            }
        //                        }
        //                    });
        //                    int dpPadding = UiUtil.dpToPx(getContext(), 5);
        //                    itemPartView.setPadding(
        //                            dpPadding, dpPadding, dpPadding, dpPadding
        //                    );
        //                    rlMoviePart.addView(itemPartView);
        //                }
        //                itemPartView.setText(infoListDTO.getInfoName());
        //                itemPartView.setIndex(i);
        //                itemPartView.setPlayStatus(infoListDTO.isPlay());
        //            }
        //        }
    }

    public String getSelectType() {
        return selectType;
    }

    public void setSelectType(String selectType) {
        this.selectType = selectType;
    }

    public interface OnSelectTypeListener {
        void onSelectType(int index, PlayListBean.PlayerListDTO infoBean);

        void onClose();
    }
}
